package life.huangzhi.community.community.service;

import jdk.nashorn.internal.ir.annotations.Reference;
import life.huangzhi.community.community.dto.PaginationDTO;
import life.huangzhi.community.community.dto.QuestionDTO;
import life.huangzhi.community.community.mapper.QuestionMapper;
import life.huangzhi.community.community.mapper.UserMapper;
import life.huangzhi.community.community.model.Question;
import life.huangzhi.community.community.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/3 21:28
 */
@Service
public class QuestionService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private UserMapper userMapper;

    /**
     *
     * 查询提问分页列表
     * @param page 页数
     * @param pageSize 每页数量
     * @return  携带分页信息的数据
     * */
    public PaginationDTO queryQuestion (Integer page, Integer pageSize) {

        PaginationDTO paginationDTO = new PaginationDTO();
        Integer questionCount = questionMapper.questionCount();
        /**页数*/
        Integer pageCount;
        //当条数不能整除每页条数的时候，添加总页数。6条总数，第一页5条。2页1条
        if(questionCount%pageSize == 0){
            pageCount = questionCount / pageSize;
        }else {
            pageCount = questionCount / pageSize + 1;
        }
        if(page < 1){
            page=1;
        }
        if(questionCount != 0&&page>pageCount){
            page=pageCount;
        }
        paginationDTO.setPagination(pageCount,page,pageSize,questionCount);
        //每页数据起始点：0/5/10/15
        Integer offset =pageSize * (page - 1);
        //装question
        List<Question> questions = questionMapper.queryQuestion(offset,pageSize);
        //装携带用户信息的question
        List<QuestionDTO> questionDTOS  = new ArrayList<>();
        for (Question question : questions) {
            User user = userMapper.findById(question.getCreator());
            QuestionDTO questionDTO = new QuestionDTO();
            //将question中的值copy给questionDTO
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            questionDTOS.add(questionDTO);
        }
        //携带翻页信息的question列表
        paginationDTO.setQuestions(questionDTOS );
        return paginationDTO;
    }

    public PaginationDTO queryQuestion(Integer userId, Integer page, Integer pageSize) {
        PaginationDTO paginationDTO = new PaginationDTO();
        Integer questionCount = questionMapper.questionCountByUserId(userId);
        /**页数*/
         Integer pageCount;
        //当条数不能整除每页条数的时候，添加总页数。6条总数，第一页5条。2页1条
        if(questionCount%pageSize == 0){
            pageCount = questionCount / pageSize;
        }else {
            pageCount = questionCount / pageSize + 1;
        }
        if(page < 1){
            page=1;
        }
        //当有个人提问且请求页数大于实际拥有页数时
        if(questionCount != 0&&page>pageCount){
            page=pageCount;
        }
        paginationDTO.setPagination(pageCount,page,pageSize,questionCount);
        //每页数据起始点：0/5/10/15
        Integer offset =pageSize * (page - 1);
        //装question
        List<Question> questions = questionMapper.queryQuestionByUserId(userId,offset,pageSize);
        //装携带用户信息的question
        List<QuestionDTO> questionDTOS  = new ArrayList<>();
        for (Question question : questions) {
            User user = userMapper.findById(question.getCreator());
            QuestionDTO questionDTO = new QuestionDTO();
            //将question中的值copy给questionDTO
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            questionDTOS.add(questionDTO);
        }
        //携带翻页信息的question列表
        paginationDTO.setQuestions(questionDTOS );
        return paginationDTO;
    }

/**问题详情页*/
    public QuestionDTO getById(Integer id) {
        Question question = questionMapper.getById(id);
        User user = userMapper.findById(question.getCreator());
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(question,questionDTO);
        questionDTO.setUser(user);
        return questionDTO;
    }
/**新增或更新问题*/
    public String createOrUpdate(Question question, User user) {
        if (question.getId() ==null){
            //新增
            question.setCreator(user.getId());
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            questionMapper.create(question);
            return "新增成功";
        }else {
            //更新
            //获取到整条数据信息。
            Question questionCopy = questionMapper.getById(question.getId());
            //判断用户是否是当前问题的发起人
            if(user.getId().equals(questionCopy.getCreator())){
                questionMapper.update(question);
                return "修改成功" ;
            }else {
                return "您没有权限！";
            }

        }
    }
}
